<?php

Class Tree extends Connect{
    
    static $all;
    
    static function getTree(){
        
        if(is_array(Tree::$all)) return Tree::$all; 
        $sql = " SELECT * FROM `".PREFIX_STATIC."tree` ";
        if(!$result = DBConnect::query($sql, 'tree')){
            $this->error(mysql_error());
            return false;
        }
        
        while($arr = DBConnect::getOne($result, 'tree')){
            if($arr['parent'] == 0){
                Tree::$all[$arr['id']]['name'] = $arr['name'];
                Tree::$all[$arr['id']]['descript'] = $arr['descript'];
                Tree::$all[$arr['id']]['id'] = intval($arr['id']);  
                Tree::$all[$arr['id']]['image'] = $arr['image'];
            }else{
                Tree::$all[$arr['parent']]['skins'][ $arr['level'] ]['level'] = $arr['level'];
                Tree::$all[$arr['parent']]['skins'][ $arr['level'] ]['image'] = $arr['image'];                                 
            }            
        }
                         
        return Tree::$all; 
    }
    
    function saveTree($data){
        $sql = "UPDATE `".PREFIX_STATIC."tree` SET `name` = '".$data['name']."', 
                                                   `descript` = '".$data['descript']."',
                                                   `image` =  '".$data['image']."'
                                                   WHERE `parent` = '0' ";
        if(!$result = DBConnect::query($sql, 'pet')){
            $this->error(mysql_error()) ;
            return false;
        }
        foreach( $data['skins'] as $k => $v ){
            $sql = "UPDATE `".PREFIX_STATIC."tree` SET `image` = '".$v['image']."' WHERE `parent` = '1' AND `level` = '".$v['level']."' ";
            DBConnect::query($sql);
        }
        return true; 
    }
    
    
    
}

?>